angular.module('start.common.messagelist.ctrl', [])

  .controller('MessageListCtrl', function($scope, $rootScope, $$MessageService, $ionicLoading, $location) {
    console.log("MessageListCtrl");
    // $scope.messageList = $$MessageService.getMessageList();
    $scope.messageList = [{}, {}];

    // 附加字段
    // pushId ：20170711001
    // pushType ：  appPath
    // appPath ：  article/12
    // textHtml ：  <h1>123</h1>




    $scope.messagelist = [];

    $scope.pageData = {
      isloading: true,
      pageSize: 10,
      pageNum: 1,
      nomoreData: false
    };

    $scope.pageData.isloading = true;

    $scope.removeMessage = function(index) {
      // 删除该信息    类型 0不删除 1删除
      $$MessageService.userupdateNotify($scope.messagelist[index].id, $scope.messagelist[index].state, 1)
        .then(function(res) {
          $ionicLoading.show({
            template: '<ion-spinner icon="ios"></ion-spinner>',
            noBackdrop: true
          });
          $scope.messagelist = [];
          $scope.pageData = {
            isloading: true,
            pageSize: 10,
            pageNum: 1,
            nomoreData: false
          };
          $scope.getmessagelistData();

        });
      // $scope.messageList.splice(($scope.messageList.length - 1 - index), 1);
      // $$MessageService.scontentAPI接口调用错误消息etMessageList($scope.messageList);
    };
    $scope.openMessage = function(index) {
      $rootScope.messageData = $scope.messagelist[index];
      $location.path("message/" + $scope.messagelist[index].id);
    };

    // $$MessageService.getuserupdateNotify($scope.messageList[index].id)
    //   .then(function(res) {

    //   });

    // $$MessageService.getuserupdateNotify()
    //   .then(function(res) {

    //   });

    $scope.getmessagelistData = function() {
      $scope.pageData.isloading = true;
      $$MessageService.getusernotify()
        .then(function(res) {
          if (res.data == null || res.data == undefined) {
            res.data = [];
          }
          if (res.errCode === 203) {
            $scope.messagelist = $scope.messagelist.concat(res.data);
            $scope.$broadcast('scroll.infiniteScrollComplete');
            // 如果没有更多数据 数据到底了
            $scope.pageData.pageNum++;
            if (res.data.length < $scope.pageData.pageSize) {
              $scope.pageData.nomoreData = true;
            } else {
              $scope.pageData.nomoreData = false;
            }
            $ionicLoading.hide();
          } else {
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
          }
          $scope.pageData.isloading = false;
        }, function(err) {
          $scope.pageData.isloading = false;
        });
    };
    $scope.getmessagelistData();

  });
